﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Tester.Models;
using UI.Presenters;
using UI.Views;

namespace Tester
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, ITestView
    {
        private readonly TestPresenter presenter;

        public MainWindow()
        {
            InitializeComponent();
            presenter = new TestPresenter(this);
            QuestionsGrid.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(AnswerChanged));
        }

        #region ITestView Members

        public void Clear()
        {
            QuestionsGrid.Children.Clear();
        }

        public void ClearVariantCombobox()
        {
            VariantComboBox.Items.Clear();
        }

        public bool AskToEndTest()
        {
            MessageBoxResult messageBoxResult = MessageBox.Show("Завершить тест?", "Вы ответили не на все вопросы",
                                                                MessageBoxButton.YesNo, MessageBoxImage.Question);
            return messageBoxResult == MessageBoxResult.Yes;
        }

        public void HideEndTestButton()
        {
            EndTestButton.Visibility = Visibility.Collapsed;
        }

        public void AddSubjectToComboBox(string subjectName)
        {
            SubjectComboBox.Items.Add(subjectName);
        }

        public void AddVariantToComboBox(int variantNumber)
        {
            VariantComboBox.Items.Add(variantNumber.ToString());
        }

        public string GetCurrentSubjectName()
        {
            return SubjectComboBox.SelectedItem.ToString();
        }

        public TestPresenter Presenter
        {
            get { return presenter; }
        }

        public void AddQuestion(string question, List<string> answers)
        {
            QuestionsGrid.RowDefinitions.Add(new RowDefinition());

            var componentFactory = new VisualComponentFactory();

            Border border = componentFactory.CreateBorder(new Point(0.5, 0), new Point(0.5, 1));

            border.SetValue(Grid.RowProperty, QuestionsGrid.RowDefinitions.Count - 1);

            border.Child = new Label
                               {
                                   Content = question,
                                   Margin = new Thickness(5),
                                   VerticalAlignment = VerticalAlignment.Center
                               };

            QuestionsGrid.Children.Add(border);

            border = componentFactory.CreateBorder(new Point(0, 0.5), new Point(1, 0.5));

            border.SetValue(Grid.RowProperty, QuestionsGrid.RowDefinitions.Count - 1);
            border.SetValue(Grid.ColumnProperty, 1);

            var stackPanel = new StackPanel {Margin = new Thickness(5)};
            foreach (string answer in answers)
                stackPanel.Children.Add(new RadioButton {Content = answer, Tag = question, Margin = new Thickness(2)});

            border.Child = stackPanel;

            QuestionsGrid.Children.Add(border);
        }

        public void ShowResult(string result)
        {
            MessageBox.Show(result);
        }

        public void AddVariantToCombobox(int number)
        {
            VariantComboBox.Items.Add(number);
        }

        #endregion

        private void StartTestButtonClick(object sender, RoutedEventArgs e)
        {
            if (VariantComboBox.SelectedIndex < 0)
                MessageBox.Show("Вариант не выбран", "Выберите вариант", MessageBoxButton.OK,
                                MessageBoxImage.Exclamation);
            else
            {
                presenter.StartTest(VariantComboBox.SelectedIndex);
                StudentInfoStackPanel.Visibility = Visibility.Collapsed;
                EndTestButton.Visibility = Visibility.Visible;
            }
        }

        public void AnswerChanged(object sender, RoutedEventArgs e)
        {
            presenter.ChangeAnswer((string) ((RadioButton) e.Source).Tag, (string) ((RadioButton) e.Source).Content);
        }

        private void EndTestButtonClick(object sender, RoutedEventArgs e)
        {
            presenter.EndTest();
        }


        private void RedactorMenuItem_OnClick(object sender, RoutedEventArgs e)
        {
            new RedactorWindow().ShowDialog();
            presenter.LoadVariant();
        }

        private void ExitButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F9)
            {
                Menu.Visibility = Visibility.Visible;
                StudentInfoStackPanel.Visibility = Visibility.Visible;
            }
        }

        private void SubjectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var subjectName = (string)SubjectComboBox.SelectedItem;

            presenter.LoadVariantsOfSubject(subjectName);
        }
    }
}